<?php

namespace scaffold\response;

use think\response\Json as TpJson;

/**
 * 响应类
 * @author edward <edward@localhost.com>
 */
class Json extends TpJson
{

    /**
     * 接口错误码
     * @var integer
     */
    protected $recode = 0;

    /**
     * 信息
     * @var string
     */
    protected $msg = 'success';

    /**
     * 数据集
     * @var array
     */
    protected $rData = array();

    /**
     * 系统函数
     * @author edward edward@localhost.com
     * @DateTime 2020-08-13
     * @version  1.0.0.0
     *
     * @param    array      $data    [description]
     * @param    integer    $code    [description]
     * @param    array      $header  [description]
     * @param    array      $options [description]
     */
    public function __construct($rData = array(), $code = 200, array $header = [], $options = [])
    {
        $this->data['recode'] = $this->recode;
        $this->data['msg']    = $this->msg;
        $this->data['data']   = $this->rData   = $rData;
        parent::__construct($this->data, $code, $header, $options);
    }

    /**
     * 信息变量赋值
     * @author edward edward@localhost.com
     * @DateTime 2020-07-17
     * @version  1.0.0.0
     *
     * @param    mixed      $name  变量名
     * @param    string     $value 变量值
     * @return   $this
     */
    public function assign($name, $value = '')
    {
        if (is_array($name)) {
            $this->rData = array_merge($this->rData, $name);
        } else {
            $this->rData[$name] = $value;
        }
        $this->data['data'] = $this->rData;
        return $this;
    }

    /**
     * 错误码
     * @author edward edward@localhost.com
     * @DateTime 2020-07-17
     * @version  1.0.0.0
     *
     * @param    integer    $recode 错误码
     * @return   $this
     */
    public function recode($recode)
    {
        $this->recode         = $recode;
        $this->data['recode'] = $recode;
        return $this;
    }

    /**
     * 信息赋值
     * @author edward edward@localhost.com
     * @DateTime 2020-07-17
     * @version  1.0.0.0
     *
     * @param    string     $msg 信息
     * @return   $this
     */
    public function msg($msg)
    {
        $this->msg         = $msg;
        $this->data['msg'] = $msg;
        return $this;
    }

}
